package com.ftg.learn.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.ftg.learn.entity.SysPermission;
import com.ftg.learn.mapper.SysPermissionMapper;
import com.ftg.learn.service.SysPermissionService;
import com.ftg.learn.util.R;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 布衣
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {

    @Resource
    SysPermissionMapper sysPermissionMapper;

    @Override
    public R<List<Tree<Long>>> list() {
        List<SysPermission> list = sysPermissionMapper.list();

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setChildrenKey("children");
        System.out.println(list);
        List<Tree<Long>> reTree = TreeUtil.build(list, 0L,treeNodeConfig,(treeNode,tree)->{
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setName(treeNode.getName());
            tree.putExtra("url",treeNode.getUrl());
            tree.putExtra("icon",treeNode.getIcon());
            tree.putExtra("type",treeNode.getType());
        });

        System.out.println(reTree);
        return R.ok(reTree);
    }

}
